package com.holike.crm.helper;

import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.holike.crm.bean.MultiItem;
import com.holike.crm.bean.NoMoreBean;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.ArrayList;
import java.util.List;

/**
 * 上拉刷新，下拉加载更多工具类
 */
public class RefreshAndLoadMoreHelper implements OnRefreshLoadMoreListener {

    private Callback mCallback;
    private SmartRefreshLayout mRefreshLayout;
    protected List<MultiItem> mListBeans;
    protected RecyclerView.Adapter mAdapter;
    protected NoMoreBean mNoMoreBean;
    protected int mPageNo, mPageSize;
    private int mStartPage;
    private int mRequestType;

    public RefreshAndLoadMoreHelper(@NonNull SmartRefreshLayout refreshLayout, @NonNull Callback callback) {
        this(refreshLayout, 1, callback);  //默认页码为1
    }

    @SuppressWarnings("WeakerAccess")
    public RefreshAndLoadMoreHelper(@NonNull SmartRefreshLayout refreshLayout, int startPage, @NonNull Callback callback) {
        this(refreshLayout, startPage, 10, callback); //默认一页加载条数为10
    }

    public RefreshAndLoadMoreHelper(@NonNull SmartRefreshLayout refreshLayout, int startPage, int pageSize, @NonNull Callback callback) {
        this.mRefreshLayout = refreshLayout;
        this.mCallback = callback;
        this.mStartPage = startPage;
        this.mPageSize = pageSize;
        this.mListBeans = new ArrayList<>();
        this.mNoMoreBean = new NoMoreBean();
        mRefreshLayout.setOnRefreshLoadMoreListener(this);
    }

    public List<MultiItem> getListBeans() {
        return mListBeans;
    }

    public void setAdapter(RecyclerView.Adapter adapter) {
        this.mAdapter = adapter;
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        onLoadMore();
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        onRefresh();
    }

    /*首次加载调用*/
    public void initData() {
        mRequestType = 0;
        mPageNo = mStartPage;
        onLoadByDefault();
    }

    public void onRefresh() {
        mRequestType = 1;
        mPageNo = mStartPage;
        onLoadByDefault();
    }

    public void onLoadMore() {
        mRequestType = 2;
        onLoadByDefault();
    }

    private void onLoadByDefault() {
        mCallback.onLoadData(mPageNo, mPageSize);
    }

    public void setResult(List<? extends MultiItem> dataList) {
        mRefreshLayout.setEnableLoadMore(true);
        mRefreshLayout.finishRefresh();
        mRefreshLayout.finishLoadMore();
        boolean isEmpty = dataList == null || dataList.isEmpty();
        boolean isLoadMoreEnabled = !isEmpty && dataList.size() >= mPageSize;
        if (mRequestType == 2) {  //加载更多
            setData(dataList, false, isLoadMoreEnabled);
        } else {
            if (isEmpty) {
                mRefreshLayout.setVisibility(View.GONE);
                mCallback.onNoResult();
            } else {
                if (mRefreshLayout.getVisibility() != View.VISIBLE) {
                    mRefreshLayout.setVisibility(View.VISIBLE);
                }
                setData(dataList, true, isLoadMoreEnabled);
            }
        }
        mPageNo++;  //加载成功后页码自增
    }

    private void setData(List<? extends MultiItem> dataList, boolean isClear, boolean isLoadMoreEnabled) {
        mCallback.onHasData();
        if (isClear) {
            mListBeans.clear();
        }
        mListBeans.addAll(dataList);
        if (!isLoadMoreEnabled) {   /*没有更多数据，添加底部“全部加载完成”*/
            this.mListBeans.remove(mNoMoreBean);
            this.mListBeans.add(mNoMoreBean);
        }
        notifyDataSetChanged();
        mRefreshLayout.setEnableLoadMore(isLoadMoreEnabled);
    }

    public void onFailure(String failReason) {
        mRefreshLayout.finishRefresh();
        mRefreshLayout.finishLoadMore();
        if (mRequestType == 0) {
            mListBeans.clear();
            notifyDataSetChanged();
            mRefreshLayout.setVisibility(View.GONE);
            mCallback.onLoadDataFailure(failReason, true);
        } else {
            mCallback.onLoadDataFailure(failReason, false);
        }
    }

    public void hide() {
        mRefreshLayout.setVisibility(View.GONE);
    }

    public void notifyDataSetChanged() {
        if (mAdapter != null) {
            mAdapter.notifyDataSetChanged();
        }
    }

    public interface Callback {

        void onLoadData(int pageNo, int pageSize);

        void onNoResult();

        void onHasData();

        void onLoadDataFailure(String failReason, boolean hide);
    }
}
